package parser

import (
	"01-crawler-project/02/engine"
	"01-crawler-project/02/entity"
	"encoding/json"
	"fmt"
	"os"
	"regexp"
)

var regex_details = `<div class="m-title" [^>]*>([\p{Han}]+)</div>`

var regex_userinfo_basicInfo = regexp.MustCompile(`<div class="m-btn purple" [^>]*>([^<]+[^>])</div>`)

var regex_userinfo_detailInfo = regexp.MustCompile(`<div class="m-btn pink" [^>]*>([^<]+[^>])</div>?`)

var regex_userinfo_choiceCondition = regexp.MustCompile(`<div class="m-btn" [^>]*>([^<]+[^>])</div>?`)

var regex_nickName = regexp.MustCompile(`<h1 class="nickName" [^>]*>(?P<nickName>[^<]+[^>])</h1>`)

var regex_Id = regexp.MustCompile(`<div class="id" [^>]*>ID：[^<](\d+)[^>]</div>`)

var regex_Introduce = regexp.MustCompile(`<div class="m-content-box m-des" [^>]*><span [^>]*>([^<]+[^>])</span><!----></div>`)

var regex_Hobby = regexp.MustCompile(`<div class="question f-fl" [^>]*>([^<]+[^>])</div>\s?<div class="answer f-fl" [^>]*>([^<]+[^>])</div>`)



func ParseUserInfo(contents [] byte) engine.ParserResult {

	result := engine.ParserResult{}

	userInfo := entity.UserInfo{}

	basicInfo := regex_userinfo_basicInfo.FindAllStringSubmatch(string(contents), -1)

	nickName := regex_nickName.FindAllStringSubmatch(string(contents), -1)

	introduce := regex_Introduce.FindAllStringSubmatch(string(contents), -1)

	detailInfo := regex_userinfo_detailInfo.FindAllStringSubmatch(string(contents), -1)

	choiceCondition := regex_userinfo_choiceCondition.FindAllStringSubmatch(string(contents), -1)

	id := regex_Id.FindAllStringSubmatch(string(contents), -1)

	if len(nickName) > 0 && len(nickName[0]) > 0 {
		userInfo.NickName = nickName[0][1]
	}

	if len(id) > 0 && len(id[0]) > 0 {
		userInfo.Id = id[0][1]
	}

	if len(introduce) > 0 && len(introduce[0]) > 0 {
		userInfo.Introduce = introduce[0][1]
	}

	userInfo.BasicInfo = make([] string, len(basicInfo))

	for i, v := range basicInfo {

		//fmt.Println(i,v)

		userInfo.BasicInfo[i] = v[1]
	}

	userInfo.DetailInfo = make([] string, len(detailInfo))

	for i, v := range detailInfo {

		userInfo.DetailInfo[i] = v[1]
	}

	userInfo.ChoiceCondition = make([] string, len(choiceCondition))

	for i, v := range choiceCondition {

		userInfo.ChoiceCondition[i] = v[1]
	}

	hobby := regex_Hobby.FindAllStringSubmatch(string(contents), -1)

	userInfo.Hobbys = make([] entity.Hobby, len(hobby))

	for k, v := range hobby {

		userInfo.Hobbys[k].Key = v[1]
		userInfo.Hobbys[k].Val = v[2]
	}

	//result := engine.ParserResult{}

	//SaveUsersToFiles(userInfo)

	//bytes, _ := json.Marshal(userInfo)
	//fmt.Printf("userInfo : %s  \n", bytes)

	result.Items = append(result.Items, userInfo)

	return result

}

func SaveUsersToFiles(userInfo entity.UserInfo) {

	path := "C:\\Users\\55219\\Desktop\\userInfos.txt"

	file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644);
	if err != nil {

		fmt.Println("OpenFileErr : ", err)
		os.Exit(1)
	}

	bytes, _ := json.Marshal(userInfo)
	fmt.Printf("userInfo : %s  \n", bytes)

	n, err := file.Write(bytes)

	file.Write([]byte{'\n'})

	if err != nil {

		fmt.Println("write userInfo Err : ", err, n)
	}

}
